 /*
ngModel : (object) { start : moment(), end : moment() }
default: (string) 今天 昨天 最近7天 最近30天 本月 上月
minDate: (string) 2016-12-02
maxDate: (string) 2016-12-02
dateLimit : (number, 限制天数) 7 
*/
var myDaterangepicker = myApp.directive('myDaterangepicker', ['$rootScope', '$parse', '$filter', function ($rootScope, $parse, $filter) {
    return {
        restrict: 'E',
        require: '?ngModel',
        link: function ($scope, element, attrs, ctrl) {
            
            var daterangeElement = element.find("div.cs-daterange");
            var dirData = {};

            
            function renderHtml(start, end) {
                var text;
                if (start || end) {
                    text = (start ? start.format('YYYY-MM-DD') : '') + ' 到 ' + (end ? end.format('YYYY-MM-DD') : '');
                } else {
                    text = '';
                }
                daterangeElement.find('span').html(text);
                daterangeElement.find('span').attr('title', text);
            }
            function setHtmlValue(start, end) {
                ctrl.$setViewValue({ start: start, end: end });
                renderHtml(start, end);
            }

            function setValue(start, end) {
                setHtmlValue(start, end);
                daterangeElement.data('daterangepicker').setStartDate(start);
                daterangeElement.data('daterangepicker').setEndDate(end);
            }
           

            ctrl.$render = function () {
                var v = ctrl.$viewValue;
                if (v && v.start && v.end) {
                    var start = moment(v.start);
                    var end = moment(v.end);
                    renderHtml(moment(v.start), moment(v.end));
                    daterangeElement.data('daterangepicker').setStartDate(start);
                    daterangeElement.data('daterangepicker').setEndDate(end);

                }
            };

            var now = moment();
            var nowDateEnd = moment().startOf('day');

            if (attrs.csForward == 'true') {
                var options = {
                    autoUpdateInput: false,
                    locale: {
                        "format": 'YYYY-MM-DD',
                        "cancelLabel": '清空',
                        "applyLabel": "确定",
                        "fromLabel": "从",
                        "toLabel": "到",
                        "customRangeLabel": "自定义",
                    },
                    ranges: {
                        '今天': [moment().startOf('day'), moment().endOf('day')],
                        '明天': [moment().add(1, 'days').startOf('day'), moment().add(1, 'days')],
                        '未来7天': [moment().endOf('day'),moment().add(6, 'days').startOf('day')],
                        '未来30天': [moment().endOf('day'),moment().add(29, 'days').startOf('day')],
                        '本月': [moment().startOf('month'), moment().endOf('month')],
                        '下月': [moment().add(1, 'month').startOf('month'), moment().add(1, 'month').endOf('month')]
                    }
                };
            }
            else if (attrs.csForwardbackward == 'true') {
                var options = {
                    autoUpdateInput: false,
                    locale: {
                        "format": 'YYYY-MM-DD',
                        "cancelLabel": '清空',
                        "applyLabel": "确定",
                        "fromLabel": "从",
                        "toLabel": "到",
                        "customRangeLabel": "自定义",
                    },
                    ranges: {
                        '最近30天': [moment().subtract(29, 'days').startOf('day'), moment().endOf('day')],
                        '最近7天': [moment().subtract(6, 'days').startOf('day'), moment().endOf('day')],
                        '昨天': [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days')],
                        '今天': [moment().startOf('day'), moment().endOf('day')],
                        '明天': [moment().add(1, 'days').startOf('day'), moment().add(1, 'days')],
                        '未来7天': [moment().endOf('day'), moment().add(6, 'days').startOf('day')],
                        '未来30天': [moment().endOf('day'), moment().add(29, 'days').startOf('day')],
                    }
                };
            }
            else {
                var options = {
                    autoUpdateInput: false,
                    locale: {
                        "format": 'YYYY-MM-DD',
                        "cancelLabel": '清空',
                        "applyLabel": "确定",
                        "fromLabel": "从",
                        "toLabel": "到",
                        "customRangeLabel": "自定义",
                    },
                    ranges: {
                        '今天': [moment().startOf('day'), moment().endOf('day')],
                        '昨天': [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days')],
                        '最近7天': [moment().subtract(6, 'days').startOf('day'), moment().endOf('day')],
                        '最近30天': [moment().subtract(29, 'days').startOf('day'), moment().endOf('day')],
                        '本月': [moment().startOf('month'), moment().endOf('month')],
                        '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                    }
                };
            }
            

            if (attrs.minDate) {
                options.minDate = attrs.minDate;
            }
            if (attrs.maxDate) {
                options.maxDate = attrs.maxDate;
            }
            if (attrs.dateLimit) {
                options.dateLimit = { days : attrs.dateLimit};
            } 

            var defaultRange = attrs.default;
            if (defaultRange) {
                var defaultDates = options.ranges[defaultRange];

                options.startDate = defaultDates[0];
                options.endDate = defaultDates[1];
            }
            daterangeElement.daterangepicker(options);
            daterangeElement.on('apply.daterangepicker', function (ev, picker) {
                setHtmlValue(picker.startDate, picker.endDate);
            });
            daterangeElement.on('cancel.daterangepicker', function (ev, picker) {
                setValue();
            });

            if (options.startDate) {
                setHtmlValue(options.startDate, options.endDate);
            }
        },
        template: '<div class="cs-daterange form-control" >' 
            + '<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>&nbsp;'
            +'<span></span>'
        +'</div>'
    };
}]);